#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTableWidget>
#include <QVector>
#include <QWidget>
#include <QPushButton>
#include <QSpinBox>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QMessageBox>
#include <QDebug>
#include <QProgressBar>
#include <QMenu>
#include <QAction>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include <QFileInfo>
#include <QDir>
#include <QProcess>
#include <QTimer>
#include <QScreen>           // Qt6 屏幕信息获取
#include <QGuiApplication>   // Qt6 GUI应用类
#include <QStringConverter>  // Qt6 字符串编码

// 前向声明Qt命名空间（如果使用UI文件）
// namespace Ui { class MainWindow; }

#ifdef Q_OS_WIN
#include <shlwapi.h> 
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shlobj.h>      // Shell文件夹操作
#include <shobjidl.h>    // Shell对象接口
#include <objbase.h>     // COM基础
#include <knownfolders.h>// 已知文件夹
#include <comdef.h>      // COM错误处理
#include <commctrl.h>    // 通用控件
#endif


// 图标信息结构体
struct IconInfo {
    QString name;
    QString filePath;
    QString type;
    qint64 size;
    QPoint position;
    bool isFolder;
    bool isHidden;
    QDateTime lastModified;
    int iconIndex;
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget* parent = nullptr);
    ~MainWindow();

private slots:
    void onScanDesktop();
    void onArrangeIcons();
    void onExportData();
    void onRefresh();
    void onTableContextMenu(const QPoint& pos);
    void openFileLocation();
    void openSelectedFile();

private:
    void setupUI();
    void setupTable();
    void createConnections();
    bool getWindowsDesktopIcons();
    void addIconToTable(const IconInfo& icon);
    QString formatFileSize(qint64 size) const;
    QPoint calculateSimulatedPosition(int index);

    // Windows平台特定实现
#ifdef Q_OS_WIN
    bool initializeCOM();
    void cleanupCOM();
    bool getDesktopShellFolder(IShellFolder** ppShellFolder);
    bool enumerateDesktopItems(IShellFolder* pShellFolder);
    QString getItemDisplayName(IShellFolder* pShellFolder, LPITEMIDLIST pidl);
    QString getItemDisplayNameFallback(IShellFolder* pShellFolder, LPITEMIDLIST pidl);
    QString getItemPath(LPITEMIDLIST pidl);
    QDateTime getItemModifiedDate(IShellFolder* pShellFolder, LPITEMIDLIST pidl);
    qint64 getItemSize(IShellFolder* pShellFolder, LPITEMIDLIST pidl);
    bool getItemAttributes(IShellFolder* pShellFolder, LPITEMIDLIST pidl, bool& isFolder, bool& isHidden);

    // 真实图标位置获取
    HWND findDesktopListViewHWND();
    int getDesktopIconCount(HWND hListView);
    QPoint getIconPosition(HWND hListView, int iconIndex);
    bool setIconPosition(HWND hListView, int iconIndex, const QPoint& position);
#endif

    QVector<IconInfo> m_iconList;

    // UI组件
    QWidget* m_centralWidget;
    QVBoxLayout* m_mainLayout;
    QHBoxLayout* m_controlLayout;
    QTableWidget* m_tableWidget;
    QPushButton* m_btnScan;
    QPushButton* m_btnArrange;
    QPushButton* m_btnExport;
    QPushButton* m_btnRefresh;
    QProgressBar* m_progressBar;
    QLabel* m_lblStatus;

    // 排列设置
    QSpinBox* m_spinColumns;
    QSpinBox* m_spinIconWidth;
    QSpinBox* m_spinIconHeight;

#ifdef Q_OS_WIN
    bool m_comInitialized;
#endif
};

#endif // MAINWINDOW_H